package com.dongnam.quanly.web;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.ActionCategory;
import com.dongnam.quanly.common.ActionType;
import com.dongnam.quanly.common.ObjectType;
import com.dongnam.quanly.common.ScreenId;

@ManagedBean(name = "AppType")
@ApplicationScoped
public class AppType implements Serializable {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(AppType.class);

	private List<SelectItem> booleanOptions;
	private List<SelectItem> trueFalseOptions;
	private List<SelectItem> screenIdOptions;
	private List<SelectItem> actionCategoryOptions;
	private List<SelectItem> actionTypeOptions;
	private List<SelectItem> resultOptions;
	private List<SelectItem> objectOptions;

	public AppType() {
		initBooleanOptions();
		initTrueFalseOptions();
		initScreenIdOptions();
		initActionCategoryOptions();
		initActionTypeOptions();
		initResultOptions();
		initObjectOptions();
	}

	public ArrayList<SelectItem> createEnumOptions(Class<?> c, boolean useId, boolean useEnumName, String idFieldName,
			boolean emptyOpt) {
		if (!c.isEnum()) {
			return null;
		}
		Object[] es = (Object[]) c.getEnumConstants();
		ArrayList<SelectItem> options = new ArrayList<SelectItem>(es.length);
		if (emptyOpt)
			options.add(new SelectItem("", ""));
		for (Object e : es) {
			try {
				if (useId) {
					Field id = e.getClass().getDeclaredField(idFieldName);
					String idValue = String.valueOf(id.get(e));
					if (useEnumName) {
						// options.add(new SelectItem(id, idValue + ": " +
						// e.toString()));
						options.add(new SelectItem(e, idValue));
					} else {
						options.add(new SelectItem(idValue, idValue));
					}
				} else {
					options.add(new SelectItem(e.toString(), e.toString()));
				}
			} catch (Exception ex) {
				logger.error("initEnumOptions", ex);
			}
		}
		return options;
	}

	public void initBooleanOptions() {
		ArrayList<SelectItem> options = new ArrayList<SelectItem>(3);
		options.add(new SelectItem("", ""));
		options.add(new SelectItem("Y", "Yes"));
		options.add(new SelectItem("N", "No"));
		booleanOptions = options;
	}

	public void initTrueFalseOptions() {
		ArrayList<SelectItem> options = new ArrayList<SelectItem>(3);
		options.add(new SelectItem(""));
		options.add(new SelectItem("True"));
		options.add(new SelectItem("False"));
		trueFalseOptions = options;
	}

	public void initScreenIdOptions() {
		screenIdOptions = createEnumOptions(ScreenId.class, false, true, "id", true);
	}

	public void initActionCategoryOptions() {
		actionCategoryOptions = createEnumOptions(ActionCategory.class, false, true, null, true);
	}

	public void initActionTypeOptions() {
		actionTypeOptions = createEnumOptions(ActionType.class, false, true, null, true);
	}

	public void initResultOptions() {
		resultOptions = new ArrayList<SelectItem>(3);
		resultOptions.add(new SelectItem("", ""));
		resultOptions.add(new SelectItem("SUCCESS", "SUCCESS"));
		resultOptions.add(new SelectItem("FAILED", "FAILED"));
	}

	public void initObjectOptions() {
		objectOptions = createEnumOptions(ObjectType.class, false, true, null, true);
	}

	public List<SelectItem> getBooleanOptionsWithEmpty() {
		return booleanOptions;
	}

	public List<SelectItem> getBooleanOptions() {
		return booleanOptions.subList(1, booleanOptions.size());
	}

	public List<SelectItem> getTrueFalseOptionsWithEmpty() {
		return trueFalseOptions;
	}

	public List<SelectItem> getTrueFalseOptions() {
		return trueFalseOptions.subList(1, trueFalseOptions.size());
	}

	public List<SelectItem> getScreenIdOptions() {
		return screenIdOptions;
	}

	public List<SelectItem> getActionCategoryOptions() {
		return actionCategoryOptions;
	}

	public List<SelectItem> getActionTypeOptions() {
		return actionTypeOptions;
	}

	public List<SelectItem> getResultOptions() {
		return resultOptions;
	}

	public List<SelectItem> getObjectOptions() {
		return objectOptions;
	}

}
